import React from "react"
import {debounce} from "../../utils/publicfun"
import "./index.less"


export default class Top extends React.Component{
    constructor (props) {
        super(props)
        this.state = {
            isShow: false
        }
    }
    componentDidMount () {
        let seft = this
        document.addEventListener("scroll", debounce(function () {
            if (document.documentElement.scrollTop > 200) {
                seft.setState({
                    isShow: true
                })
            } else {
                seft.setState({
                    isShow: false
                })
            }
        }, 200))
    }
    scrollToTop = () => {
        (function scrollTop () {
            var currentScroll = document.documentElement.scrollTop || document.body.scrollTop;
            if (currentScroll > 0) {
                window.requestAnimationFrame(scrollTop);
                window.scrollTo (0,currentScroll - (currentScroll/5));
            }
        })()
    }
    render () {
        if (this.state.isShow) {
            return (<div className="top"  onClick={this.scrollToTop}>
                <span className="jz iconprev"></span>
            </div>)
        } else {
            return <div></div>
        }
    }
}